/*
 * CommunicationRobot12.cpp
 *
 *  Created on: 15/11/2013
 *      Author: gonzalo
 */

#include "CommunicationRobot12.h"


namespace middleware {

const std::string CommunicationRobot12::ROBOT12_SEM_PATH = "/tmp/robot12";
const std::string CommunicationRobot12::ROBOT12_SHMEM_PATH = "/tmp/robot12mem";

CommunicationRobot12::CommunicationRobot12(char key) : CommunicationRobotPiezas(key) {
	this->semRobot = ipc::Semaphore::get(ROBOT12_SEM_PATH, this->key);
	this->shMemListo = ipc::SharedMemoryInterface<Robot12Listo>::get(ROBOT12_SHMEM_PATH, this->key);
	this->shMemEstado = ipc::SharedMemoryInterface<EstadoRobotPiezas>::get(ROBOT12_SHMEM_PATH, CommunicationRobotPiezas::getAltKey(this->key));
}

void CommunicationRobot12::setProcesando() {
	this->shMemListo.lock();
	this->shMemListo.write(false);
	this->shMemListo.unlock();
}

void CommunicationRobot12::setListo() {
	this->shMemListo.lock();
	this->shMemListo.write(true);
	this->shMemListo.unlock();
}

bool CommunicationRobot12::estaListo() {
	bool listo;
	this->shMemListo.lock();
	listo = this->shMemListo.read();
	this->shMemListo.unlock();
	return listo;
}

void CommunicationRobot12::create(char key) {
	ipc::Semaphore::create(ROBOT12_SEM_PATH, key).initialize(0);
	ipc::SharedMemoryInterface<Robot12Listo>::create(ROBOT12_SHMEM_PATH, key);
	ipc::SharedMemoryInterface<EstadoRobotPiezas>tmpMem = ipc::SharedMemoryInterface<EstadoRobotPiezas>::create(ROBOT12_SHMEM_PATH, CommunicationRobotPiezas::getAltKey(key));
	tmpMem.lock();
	tmpMem.write(ROBOT_INACTIVO);
	tmpMem.unlock();
}

void CommunicationRobot12::destroy(char key) {
	ipc::Semaphore::destroy(ROBOT12_SEM_PATH, key);
	ipc::SharedMemoryInterface<Robot12Listo>::destroy(ROBOT12_SHMEM_PATH, key);
	ipc::SharedMemoryInterface<EstadoRobotPiezas>::destroy(ROBOT12_SHMEM_PATH, CommunicationRobotPiezas::getAltKey(key));
}

std::string CommunicationRobot12::getNombre() {
	return "robot 12";
}

CommunicationRobot12::~CommunicationRobot12() {
}

} /* namespace middleware */
